package www.yingdi365.com.activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.amap.api.maps2d.model.LatLng;
import com.lzy.okgo.model.HttpHeaders;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.model.QingJianBean;
import www.yingdi365.com.model.XiuGaiQJWZ;
import www.yingdi365.com.util.IsMobileNOorPassword;
import www.yingdi365.com.util.Lunar;

public class XiuGaiQJWZActivity extends ZjbBaseActivity implements View.OnClickListener {
    private TextView textViewRight;
    private EditText editBoy;
    private EditText editBoyMobile;
    private EditText editGirl;
    private EditText editGirlMobile;
    private EditText editHotel;
    private EditText editTreatAddress;
    private TextView textTreatDate;
    private TextView textTreatAddressLocation;
    private XiuGaiQJWZ xiuGaiQJWZ;
    private QingJianBean pagesBean;
    private int position;
    private String time;
    private String nongli;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiu_gai_qjwz);
        init();
    }
    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        xiuGaiQJWZ = (XiuGaiQJWZ) intent.getSerializableExtra(Constant.INTENT_KEY.XiuGaiQJWZ);
        pagesBean = (QingJianBean) intent.getSerializableExtra(Constant.INTENT_KEY.pagesBean);
        position = intent.getIntExtra(Constant.INTENT_KEY.Position, -1);
        time = pagesBean.getItems().get(6).getText().getContent();
        nongli = pagesBean.getItems().get(7).getText().getContent();
    }

    @Override
    protected void findID() {
        textViewRight = (TextView) findViewById(R.id.textViewRight);
        editBoy = (EditText) findViewById(R.id.editBoy);
        editBoyMobile = (EditText) findViewById(R.id.editBoyMobile);
        editGirl = (EditText) findViewById(R.id.editGirl);
        editGirlMobile = (EditText) findViewById(R.id.editGirlMobile);
        editHotel = (EditText) findViewById(R.id.editHotel);
        editTreatAddress = (EditText) findViewById(R.id.editTreatAddress);
        textTreatDate = (TextView) findViewById(R.id.textTreatDate);
        textTreatAddressLocation = (TextView) findViewById(R.id.textTreatAddressLocation);
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("修改请柬");
        textViewRight.setText("保存");
        editBoy.setText(xiuGaiQJWZ.getBoy());
        editBoy.setSelection(xiuGaiQJWZ.getBoy().length());
        editBoyMobile.setText(xiuGaiQJWZ.getBoyMobile());
        editGirl.setText(xiuGaiQJWZ.getGirl());
        editGirlMobile.setText(xiuGaiQJWZ.getGirlMobile());
        editHotel.setText(xiuGaiQJWZ.getHotel());
        editTreatAddress.setText(xiuGaiQJWZ.getTreatAddress());
        textTreatDate.setText(xiuGaiQJWZ.getTreatDate());
        textTreatAddressLocation.setText(xiuGaiQJWZ.getTreatAddressLocation());
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        findViewById(R.id.textViewRight).setOnClickListener(this);
        findViewById(R.id.viewDiTu).setOnClickListener(this);
        textTreatDate.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_RESULT_CODE.POSITION && resultCode == Constant.REQUEST_RESULT_CODE.POSITION) {
            LatLng position = data.getParcelableExtra(Constant.INTENT_KEY.Position);
            textTreatAddressLocation.setText(position.latitude + "," + position.longitude);
        }
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.AppInvitationService + "invitation/save";
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers, params, url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.viewDiTu:
                Intent intent = new Intent();
                intent.setClass(this, DiTuActivity.class);
                startActivityForResult(intent, Constant.REQUEST_RESULT_CODE.POSITION);
                break;
            case R.id.textTreatDate:
                Calendar c = Calendar.getInstance();
                new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, final int year, final int month, final int dayOfMonth) {
                        new TimePickerDialog(XiuGaiQJWZActivity.this, new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                textTreatDate.setText(year + "-" + (month + 1) + "-" + dayOfMonth+" "+hourOfDay+":"+minute);
                                time=year+"年"+(month+1)+"月"+dayOfMonth+"日 "+hourOfDay+"时"+minute+"分";
                                String str=year + "-" + (month + 1) + "-" + dayOfMonth;
                                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
                                Date date = null;
                                try {
                                    date = sdf.parse(str);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                Calendar calendar = Calendar.getInstance();
                                calendar.setTime(date);
                                Lunar lunar = new Lunar(calendar);
                                nongli = lunar.toString();
                            }
                        },12,0,true).show();
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
                break;
            case R.id.textViewRight:
                if (TextUtils.isEmpty(editBoy.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请填写新郎名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!IsMobileNOorPassword.isMobileNO(editBoyMobile.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请正确填写新郎手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editGirl.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请填写新娘", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!IsMobileNOorPassword.isMobileNO(editGirlMobile.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请正确填写新娘手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(textTreatDate.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请选择婚礼日期", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editHotel.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请填写酒店名称", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editTreatAddress.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请填写婚礼地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(textTreatAddressLocation.getText().toString().trim())) {
                    Toast.makeText(XiuGaiQJWZActivity.this, "请选择婚礼坐标", Toast.LENGTH_SHORT).show();
                    return;
                }
                pagesBean.getItems().get(4).getText().setContent(editBoy.getText().toString().trim());
                pagesBean.getItems().get(5).getText().setContent(editGirl.getText().toString().trim());
                pagesBean.getItems().get(6).getText().setContent(time);
                pagesBean.getItems().get(7).getText().setContent(nongli);
                pagesBean.getItems().get(8).getText().setContent(editHotel.getText().toString().trim());
                XiuGaiQJWZ xiuGaiQJWZ = new XiuGaiQJWZ(
                        editBoy.getText().toString().trim(),
                        editBoyMobile.getText().toString().trim(),
                        editGirl.getText().toString().trim(),
                        editGirlMobile.getText().toString().trim(),
                        textTreatDate.getText().toString().trim(),
                        editHotel.getText().toString().trim(),
                        editTreatAddress.getText().toString().trim(),
                        textTreatAddressLocation.getText().toString().trim()
                );
                Intent intent01 = new Intent();
                intent01.setAction(Constant.BROADCASTCODE.XiuGaiQJWZ);
                intent01.putExtra(Constant.INTENT_KEY.XiuGaiQJWZ,xiuGaiQJWZ);
                intent01.putExtra(Constant.INTENT_KEY.pagesBean,pagesBean);
                intent01.putExtra(Constant.INTENT_KEY.Position,position);
                sendBroadcast(intent01);
                finish();
                break;
            case R.id.imageBack:
                finish();
                break;
        }
    }
}
